'use strict';

const Controller = require('egg').Controller;
const { records } = require('../mocks/index');

function sleep(time) {
  return new Promise(resolve => setTimeout(resolve, time));
}

function paginate(array, page_size, page_number) {
  // human-readable page numbers usually start with 1, so we reduce 1 in the first argument
  return array.slice((page_number - 1) * page_size, page_number * page_size);
}

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }

  async form() {
    const { ctx } = this;
    // await sleep(300);
    ctx.body = {
      success: true,
      message: '',
      code: 200,
      result: {
        text: {
          type: 'string',
          ui: {
            widget: 'CusInput',
            label: '文字',
            widgetConfig: {},
            watch: [
              'maxText',
            ],
          },
          rules: [],
        },
        maxText: {
          type: 'string',
          ui: {
            widget: 'CusInput',
            label: '最长10个文字',
            widgetConfig: {
              maxlength: 10,
            },
          },
          rules: [],
        },
      },
    };
  }

  async detail2() {
    const { ctx } = this;
    ctx.body = {
      success: true,
      message: '',
      code: 200,
      result: {
        data: {
          text: 'test',
          arr: [
            {
              radio1: '1',
              text: '111',
            },
          ],
        },
      },
    };
  }

  async form2() {
    const { ctx } = this;
    ctx.body = {
      success: true,
      message: '',
      code: 200,
      result: {
        text: {
          type: 'string',
          ui: {
            area: 'A2',
            widget: 'CusInput',
            label: '文字',
            widgetConfig: {},
            // watch: [
            //   'maxText',
            // ],
          },
          rules: [],
        },
        maxText: {
          type: 'string',
          ui: {
            area: 'A3',
            widget: 'CusInput',
            label: '最长10个文字',
            widgetConfig: {
              maxlength: 10,
            },
          },
          rules: [],
        },
        arr: {
          type: 'array',
          ui: {
            area: 'A1',
          },
          items: {
            type: 'object',
            properties: {
              text: {
                type: 'string',
                ui: {
                  widget: 'CusInput',
                  label: '文字',
                  widgetConfig: {},
                  watch: [
                    'maxText',
                  ],
                },
                rules: [],
              },
              maxText: {
                type: 'string',
                ui: {
                  widget: 'CusInput',
                  label: '最长10个文字',
                  widgetConfig: {
                    maxlength: 10,
                  },
                },
                rules: [],
              },
              number: {
                type: 'number',
                ui: {
                  widget: 'CusInputNumber',
                  label: '数字',
                  widgetConfig: {
                    type: 'number',
                  },
                },
                rules: [],
              },
              integer: {
                type: 'number',
                ui: {
                  widget: 'CusInput',
                  label: '整数',
                  widgetConfig: {
                    type: 'number',
                    subType: 'integer',
                  },
                },
                rules: [],
              },
              text1: {
                type: 'string',
                ui: {
                  widget: 'CusInput',
                  label: '文字',
                  widgetConfig: {},
                },
                rules: [],
              },
              select: {
                type: 'string',
                ui: {
                  widget: 'CusSelect',
                  label: '下拉',
                  widgetConfig: {
                    options: [
                      {
                        label: '选项1',
                        value: 'option1',
                      },
                      {
                        label: '选项2',
                        value: 'option2',
                      },
                    ],
                  },
                },
                rules: [],
              },
              radio1: {
                type: 'string',
                ui: {
                  // area: 'A2',
                  widget: 'CusCheckSwitch',
                  label: '单选1',
                  widgetConfig: {
                  },
                },
                rules: [],
              },
              radio2: {
                type: 'string',
                ui: {
                  // area: 'A1',
                  widget: 'CusCheckSwitch',
                  label: '单选2',
                  widgetConfig: {
                  },
                },
                rules: [],
              },
            },
          },
        },
      },
    };
  }

  async empty_list() {
    const { ctx } = this;
    await sleep(300);
    const limit = parseInt(ctx.query.limit);
    const page = parseInt(ctx.query.page);
    // console.log(limit);
    // console.log(size, page);
    ctx.body = {
      success: true,
      message: '',
      code: 200,
      result: {
        records: [],
      },
    };
  }


  async list() {
    const { ctx } = this;
    await sleep(300);
    const limit = parseInt(ctx.query.limit);
    const page = parseInt(ctx.query.page);
    // console.log(limit);
    // console.log(size, page);
    ctx.body = {
      success: true,
      message: '',
      code: 200,
      result: {
        columns: [
          [ '源数据库名', 'sourceDbName' ],
          [ '源数据库表', 'sourceTable' ],
          [ '目标数据库名', 'targetDbName' ],
          [ '目标数据库表', 'targetTable' ],
        ],
        columnMap: {
          sourceDbName: {
            type: 'string',
            ui: {
              // label: '数据库名',
              widget: 'CusInput',
            },
            quick: true,
          },
          testAbc: {
            type: 'string',
            ui: {
              label: 'testAbc',
              widget: 'CusInput',
            },
          },
        },
        records: paginate(records, limit, page),
        total: records.length,
        size: limit,
        current: page,
        orders: [],
        optimizeCountSql: true,
        searchCount: true,
        countId: null,
        maxLimit: null,
        pages: 2,
      }, timestamp: 1648177923016,
    };
  }

  async dicts() {
    const { ctx } = this;
    // await sleep(300);
    ctx.body = {
      success: true,
      message: '',
      code: 200,
      result: {
        options: [
          {
            label: '选项11',
            value: 'option11',
          },
          {
            label: '选项12',
            value: 'option12',
          },
        ],
      },
    };
  }

  async options() {
    const { ctx } = this;
    // await sleep(300);
    ctx.body = {
      success: true,
      message: '',
      code: 200,
      result: {
        options1: [
          {
            label: '选项11',
            value: 'option11',
          },
          {
            label: '选项12',
            value: 'option12',
          },
        ],
        options2: [
          {
            label: '选项21',
            value: 'option21',
          },
          {
            label: '选项22',
            value: 'option22',
          },
        ],
      },
    };
  }

  async upload() {
    const { ctx } = this;
    // await sleep(300);
    ctx.body = {
      success: true,
      message: '',
      code: 200,
      result: {},
    };
  }

  async edit() {
    const { ctx } = this;
    // await sleep(300);
    // console.log(ctx.request);
    ctx.body = {
      success: true,
      message: '',
      code: 200,
      result: {
        request: ctx.request,
        body: ctx.request.body,
      },
    };
  }

}

module.exports = HomeController;
